//
//  MoveDetailFrameModel.m
//  练习06
//
//  Created by hcios on 2017/7/7.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "MoveDetailFrameModel.h"

#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width

#define TitleFont [UIFont systemFontOfSize:15]
#define ContextFont [UIFont systemFontOfSize:13]

@implementation MoveDetailFrameModel

-(void)setModel:(MoveDetailModel *)model{
    _model = model;
    
    //图标
    CGFloat padding = 10;
    self.setTitleViewFrame = CGRectMake(padding, padding, 3, 18);
    
    //标题
    if ([model.title isEqualToString:@""]) {
        self.titleFrame = CGRectZero;
    }else{
        CGSize titleSize = [self sizeWithText:model.title font:TitleFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
        CGFloat titleW = titleSize.width;
        CGFloat titleH = titleSize.height;
        self.titleFrame = CGRectMake(CGRectGetMaxX(self.setTitleViewFrame) + padding, padding, titleW, titleH);
    }
    
    //图像
    if([model.image_width isEqualToString:@"0"]){
        self.imageFrame = CGRectMake(padding, CGRectGetMaxY(self.titleFrame) + 5, 0, 0);
    }else{
        CGFloat iconOW = [model.image_width integerValue];
        CGFloat iconOH = [model.image_height integerValue];
        CGFloat bili = iconOH / iconOW;;
        CGFloat iconX = padding;
        CGFloat iconY = CGRectGetMaxY(self.titleFrame) + 5;
        CGFloat iconW = SCREEN_WIDTH - (2 * padding);
        CGFloat iconH = bili * iconW;
        self.imageFrame = CGRectMake(iconX, iconY, iconW, iconH);
        //姓名
        self.user_nameFrame = CGRectMake(padding, iconH - 50, iconW, 20);
        //旅行地
        self.trip_nameFrame = CGRectMake(padding, iconH - 25, iconW, 20);
        
    }
    
    //文本
    if (![model.image_url isEqualToString:@""]) {
        CGSize textSize = [self sizeWithText:model.description_text font:ContextFont maxSize:CGSizeMake(SCREEN_WIDTH - padding * 2, MAXFLOAT)];
        self.contextFrame = CGRectMake(padding, CGRectGetMaxY(self.imageFrame) + 5, textSize.width, textSize.height);
    }else{
        CGSize textSize = [self sizeWithText:model.description_text font:ContextFont maxSize:CGSizeMake(SCREEN_WIDTH - padding * 2, MAXFLOAT)];
        self.contextFrame = CGRectMake(padding, CGRectGetMaxY(self.titleFrame) + 10, textSize.width, textSize.height);
    }
    
    //
    if (![model.name isEqualToString:@""]) {
        self.buttonLabelFrame = CGRectMake(padding, CGRectGetMaxY(self.contextFrame), SCREEN_WIDTH, 20);
    }else{
        self.buttonLabelFrame = CGRectMake(padding, CGRectGetMaxY(self.contextFrame), SCREEN_WIDTH, 0);
    }
    _cellHeight = CGRectGetMaxY(self.buttonLabelFrame) + padding;
    
}


-(CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize{
    NSDictionary *dict = @{NSFontAttributeName:font};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
}


@end
